const Joi = require('joi');


class BarcodeValidate {
  // 条码列表参数校验
  async list(params) {

    const schema = Joi.object({
      product_id: Joi.string(),
      name: Joi.string()
        .pattern(/^[^\s]+$/)
        .messages({
          'string.pattern': '名称不能包含特殊字符',
        }),
      barcode: Joi.string(),
      method: Joi.string().default('desc'),
      order: Joi.string().default('create_time'),
      pageIndex: Joi.string().default('1'),
      pageSize: Joi.string().default('10'),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }


  // 新增条码参数校验
  async insert(params) {

    const schema = Joi.object({
      product_id: Joi.string()
        .required()
        .messages({
          'string.empty': '产品id不能为空',
          'string.required': '产品id为必填项',
        }),

      barcode: Joi.string()
        .required()
        .messages({
          'string.empty': '编码不能为空',
          'string.required': '编码是必填项',
        }),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  // 删除条码参数校验
  async delete(params) {

    const schema = Joi.object({
      ids: Joi.array(),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }
}


module.exports = new BarcodeValidate();
